package palmtop.com.palmtop.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;

import com.beardedhen.androidbootstrap.AwesomeTextView;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;

import java.util.ArrayList;

import palmtop.com.palmtop.R;
import palmtop.com.palmtop.activity.ClueAcountActivity;
import palmtop.com.palmtop.activity.ClueActivity;
import palmtop.com.palmtop.activity.WxClueActivity;
import palmtop.com.palmtop.model.Clue;
import palmtop.com.palmtop.model.Reportpopulation;
import palmtop.com.palmtop.util.BadgeView;
import palmtop.com.palmtop.util.Config;
import palmtop.com.palmtop.util.NetUtil;

/**
 * Created by Administrator on 2017/9/1.
 */

public class ClueacountAdapter extends BaseAdapter {
    private ArrayList<Clue> arrlist;
    private Context context;

    public ClueacountAdapter(ArrayList<Clue> arrlist, Context context) {
        this.arrlist = arrlist;
        this.context = context;
    }

    @Override
    public int getCount() {
        return arrlist.size();
    }

    @Override
    public Object getItem(int i) {
        return arrlist.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int pos, View view, ViewGroup viewGroup) {
        view = LayoutInflater.from(context).inflate(R.layout.item_clueacount,null);
        AwesomeTextView unitname = view.findViewById(R.id.unitname);
        AwesomeTextView sum = view.findViewById(R.id.sum);
        AwesomeTextView taskname1 = view.findViewById(R.id.taskname1);

        Button xiangqing = view.findViewById(R.id.xiangqing);
        final Clue clue = arrlist.get(pos);
        setUnreadsum(clue.getUnitid(),taskname1);
        unitname.setText(clue.getUnitname());
        sum.setText(clue.getUnitcount());
        xiangqing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.putExtra("clue",clue);
                if(clue.getUnitid().equals("0")){
                    intent.setClass(context,WxClueActivity.class);
                }else{
                    intent.setClass(context,ClueActivity.class);
                }
//                intent.setClass(context,ClueActivity.class);
                context.startActivity(intent);
            }
        });



        return view;
    }

    private void setUnreadsum(String unitid,final AwesomeTextView taskname1) {
        /*BadgeView badge = new BadgeView(context,taskname1);
        badge.setText("12");
        badge.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
        badge.show();*/
        RequestParams params = new RequestParams(Config.SERVER_URL + "clue/getCluecountByUnitid");
        params.addBodyParameter("unitid",unitid);
        NetUtil.postData(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if(!(result==null||result.equals("\"\""))){
                    BadgeView badge = new BadgeView(context,taskname1);
                    badge.setText(result);
                    badge.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
                    badge.show();
                }
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }
            @Override
            public void onCancelled(CancelledException cex) {
            }
            @Override
            public void onFinished() {
            }
        });


    }


}
